package com.project.service.impl;

import com.project.bean.UserBean;
import com.project.mapper.IUserDao;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {

    @Override
    public UserBean login(String account, String pwd) {
        UserBean userBean;
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        userBean = userDao.login(account,pwd);

        session.close();
        return userBean;
    }

    @Override
    public List<UserBean> findAllUser() {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        List<UserBean> list = userDao.findAllUser();
        session.close();
        return list;
    }

    @Override
    public boolean addUser(UserBean user) {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        UserBean userBean = userDao.checkName(user.getAccount());
        if (userBean==null){
            userDao.addUser(user);
            session.commit();
            session.close();
            return true;
        }
        return false;
    }

    @Override
    public void delUser(int id) {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        userDao.delUser(id);
        session.commit();
        session.close();
    }

    @Override
    public UserBean findById(Integer id) {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        UserBean userBean = userDao.findById(id);
        session.close();
        return userBean;
    }

    @Override
    public void updateRank(int id, String rank) {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        userDao.updateRank(id,rank);
        session.commit();
        session.close();
    }

    @Override
    public void updatePwd(Integer id,String newPwd) {
        SqlSession session = this.getSession();
        IUserDao userDao = session.getMapper(IUserDao.class);
        userDao.updatePwd(id,newPwd);
        session.commit();
        session.close();
    }
}
